<?php

/*
 * CopyRight  : (C)2012-2099 HaoTeam Inc.
 * Document   : AdminUserNode.php
 * Created on : 2021-1-30 13:26:32
 * Author     : Tiger <1192851302@qq.com>
 * Description: This is NOT a freeware, use is subject to license terms.
 *              这即使是一个免费软件,使用时也请遵守许可证条款,得到当时人书面许可.
 *              未经书面许可,不得翻版,翻版必究;版权归属 HaoTeam Inc;
 */

namespace app\admin\model;

use think\Model;
use haoteam\service\AdminService;

/**
 * 
 * Class AdminUserNode
 * Tiger <1192851302@qq.com> 2021-1-30 13:26:32
 */
class AdminUserNode extends Model {

    /**
     * 获取相关用户的权限
     * @author Tiger <1192851302@qq.com>
     * @param type $id
     * @return type
     */
    public static function getTree($id) {
        return AdminService::instance()->getTree(self::where(['uid' => $id])->column('node') ?? []);
    }

    /**
     * 保存用户权限
     * @author Tiger <1192851302@qq.com>
     * @param type $id
     * @return type
     */
    public static function createData($id, $nodes = []) {
        $data = [];
        foreach ($nodes ?? [] as $node) {
            $data[] = ['uid' => $id, 'node' => $node];
        }
        
        self::where(['uid' => $id])->delete();
        if (!empty($data)) {
            self::insertAll($data);
        }
        return true;
    }

}
